package net.sky.oa.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.GregorianCalendar;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;

public class FileUtil {
	
	
	//上传文件返回url
	public static String upload(File file,String realPath,String parentPath,String fileName) throws Exception {
		
		File folder = new File(realPath);	
		if(!folder.isDirectory()) {
			if(!folder.exists()) 
				folder.mkdirs();			
		}
		
		String name = String.valueOf(new GregorianCalendar().getInstance().getTimeInMillis());
		name = name+fileName.substring(fileName.lastIndexOf("."));
		
		FileInputStream in = new FileInputStream(file);		
		FileOutputStream out = new FileOutputStream(realPath+"\\"+name);
		
		byte[] buffer = new byte[2048];
		int len = 0;
		while ((len = in.read(buffer)) >0) {
			out.write(buffer, 0, len);
		}
		
		out.close();
		in.close();
		return parentPath+"/"+name;
	}
	
	// 上传文件
	public static void upload(InputStream is, String path) throws Exception {
		//上传图片		
		OutputStream ops = new FileOutputStream(path);
		
		byte[] buffer = new byte[2048];
		int len = 0;
		while ((len = is.read(buffer)) >0) {
			ops.write(buffer, 0, len);
		}
		ops.close();
		is.close();	
		
	}
	
	public static void upload(InputStream is, String parentpath,String filename ) {
		File folder = new File(parentpath);
		if(!folder.isDirectory()) {
			if(!folder.exists()) {
				folder.mkdirs();
			}
		}
		
		String path = parentpath+filename;
		File file1 = new File(path);
		try {
			
			OutputStream ops = new FileOutputStream(file1);
			
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
				ops.write(buffer, 0, bytesRead);
			}
			ops.close();
			is.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	
	//写入文本内容到文件
	/*
	public static void writeContentToFile(String content,String parentpath,String filepath) {
		FileOutputStream fos  = null;	
		BufferedInputStream bis = null;
		ByteArrayInputStream bais = null;
		try {
			File file = new File(parentpath);
			file.mkdir();
			bais = new ByteArrayInputStream(content.getBytes());
			bis = new BufferedInputStream(bais);
			fos = new FileOutputStream(parentpath+filepath);
			
			int bytesRead = 0;
			byte[] buffer = new byte[8192];
			while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
				fos.write(buffer, 0, bytesRead);
			}
			fos.close();
			bis.close();
			bais.close();
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	*/

	public static void writeContentToFile(String content,String parentpath,String filepath) {
			
		File folder = new File(parentpath);
		if(!folder.isDirectory()) {
			if(!folder.exists()) {
				folder.mkdirs();
			}
		}
			
			File file = new File(parentpath+filepath);
			
			try {
				FileUtils.writeStringToFile(file, content, "gb2312");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
	}
	public static String readContentFromFile(String filepath) {		
		String content = null;
		try {
			File file = new File(filepath);
			content = FileUtils.readFileToString(file, "gb2312");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}
	
	
	/*------------------------------------下载---------------------------------*/
	public static void downLoadNormalFile(String filePath,String fileName,String saveFileName,HttpServletResponse m_response){   
		  
        //File file = new File(filePath+"/"+fileName);  
        String path = filePath+"/"+fileName;
        System.out.println("path---"+path);   
        File file = new File(path);
        if(saveFileName==null||"".equals(saveFileName.trim())){   
            saveFileName = fileName;   
        }   
        try{   
        	 saveFileName = URLEncoder.encode(saveFileName,"UTF-8");   
             m_response.setContentType("application/x-msdownload");   
             m_response.setHeader("Content-Disposition", "attachment;"+ " filename=" + saveFileName);   
             ServletOutputStream sos = null;   
             FileInputStream fis = null;   
             byte[] b = new byte[1024];   
             fis = new FileInputStream(file);   
             sos = m_response.getOutputStream();   
             int len=0;   
               while ( (len = fis.read(b)) > 0) {   
                 sos.write(b,0, len);    
                }   
                 sos.close();   
                 fis.close();   
         }catch(Exception ex){   
             ex.printStackTrace();              
         }   
    }  
	/*
	public static void downLoadNormalFile(String filePath,String fileName,String saveFileName,HttpServletResponse m_response){   
		
		//File file = new File(filePath+"/"+fileName);  
		String path = filePath+"/"+fileName;
		System.out.println("path---"+path);
		URL url = null;
		try {
			url = new URL(path);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(saveFileName==null||"".equals(saveFileName.trim())){   
			saveFileName = fileName;   
		}   
		try{   
			
			saveFileName = URLEncoder.encode(saveFileName,"UTF-8");   
			m_response.setContentType("application/x-msdownload");   
			m_response.setHeader("Content-Disposition", "attachment;"+ " filename=" + saveFileName);   
			ServletOutputStream sos = null;   
			//FileInputStream fis = null;   
			byte[] b = new byte[10000];   
//            fis = new FileInputStream(file);   
//            fis = new FileInputStream(url.openStream());   
			BufferedInputStream bis = new BufferedInputStream(url.openStream());
			sos = m_response.getOutputStream();   
			int len=0;   
//              while ( (len = fis.read(b)) > 0) {   
			while ( (len = bis.read(b)) > 0) {   
				sos.write(b,0, len);    
			}   
			sos.flush();
			
			//sos.close();   
			bis.close();
			//fis.close();   
		}catch(Exception ex){   
			ex.printStackTrace();              
		}   
	}  
	*/
	
	public static void downLoadNormalFile(String filePath,HttpServletResponse m_response){   
		
		File file = new File(filePath);   
		if(!file.exists()){   
			return ;   
		}   
		
		try{   			  
			m_response.setContentType("application/x-msdownload");   			
			ServletOutputStream sos = null;   
			FileInputStream fis = null;   
			byte[] b = new byte[1024];   
			fis = new FileInputStream(file);   
			sos = m_response.getOutputStream();   
			int len=0;   
			while ( (len = fis.read(b)) > 0) {   
				sos.write(b,0, len);    
			}   
			sos.close();   
			fis.close();   
		}catch(Exception ex){   
			ex.printStackTrace();              
		}   
	}   

	

}
